package com.zl.cms.dao;

import com.google.common.base.Optional;
import com.zl.basic.CommonUtil.PinyinUtil;
import com.zl.basic.dao.BaseDao;
import com.zl.basic.model.Pager;
import com.zl.cms.model.Keyword;
import org.springframework.stereotype.Repository;

import java.util.*;

/**
 * User: zl email:c3tc3tc3t@126.com Date: 14-3-7 Time: 下午10:08
 */
@Repository("keywordDao")
public class KeywordDao extends BaseDao<Keyword> implements IKeywordDao {


  @Override
  public List<Keyword> listChiefKeyword(int num) {
    String hql = "from Keyword where times >= ?";
    return this.list(hql, num);
  }

  @Override
  public void addOrUpdate(String name) {
    Keyword k = (Keyword) this.queryObjectByHql("from Keyword where name = ?", name);
    Optional<Keyword> keywordOptional = Optional.fromNullable(k);
    if (keywordOptional.isPresent()) {
      k.setTimes(k.getTimes() + 1);
    } else {
      k = new Keyword();
      k.setName(name);
      k.setNameFullPy(PinyinUtil.str2Pinyin(name, null));
      k.setNameShortPy(PinyinUtil.strFirst2Pinyin(name));
      k.setTimes(1);
      this.add(k);
    }
  }

  @Override
  public Pager<Keyword> findNoUseKeyword() {
    String hql = "from Keyword where name not in (:ks)";
    // 得到一组存在的关键字名称
    Set<String> ks = getExistKeywords();
    Map<String, Object> alias = new HashMap<>();
    alias.put("ks", ks);
    return this.findByAlias(hql, alias);
  }

  @SuppressWarnings("unchecked")
  private Map<String, Integer> getExistKeyword2Map() {
    String hql = "select t.keyword from Topic t ";

    List<String> allKeys = this.getSession().createQuery(hql).list();
    Map<String, Integer> keys = new HashMap<>();

    for (String key : allKeys) {
      String[] ks = key.split("\\|");
      for (String k : ks) {
        if ("".equals(k)) continue;
        if (keys.containsKey(k)) {
          keys.put(k, keys.get(k) + 1);
        } else {
          keys.put(k, 1);
        }
      }
    }
    return keys;
  }


  @SuppressWarnings("unchecked")
  private Set<String> getExistKeywords() {
    return getExistKeyword2Map().keySet();
  }

  @Override
  public void clearNoUseKeyword() {
    String hql = "delete Keyword  k where k .name not in (:ks)";

    Set<String> ks = getExistKeywords();

    this.getSession().createQuery(hql).setParameterList("ks", ks).executeUpdate();
  }


  @Override
  public List<Keyword> findUseKeyword() {
    Map<String, Integer> allkeys = getExistKeyword2Map();
    List<Keyword> ks = new ArrayList<>();

    Set<String> keys = allkeys.keySet();
    for (String key : keys) {
      ks.add(new Keyword(key, allkeys.get(key)));
    }
    Collections.sort(ks);
    return ks;
  }

  @Override
  public List<Keyword> listKeywordByCon(String con) {
    String param = "%" + con + "%";
    String hql = "from Keyword where name like ? or nameFullPy like ?  or nameShortPy like ?";

    return this.list(hql, new Object[] {param, param, param});
  }

  @Override
  public List<String> listKeywordStringByCon(String con) {
    String param = "%" + con + "%";
    String hql = "from Keyword where name like ? or nameFullPy like ?  or nameShortPy like ?";
    List<Keyword> rs = this.list(hql, new Object[] {param, param, param});
    List<String> names = new ArrayList<>();
	for(Keyword k : rs) {
		names.add(k.getName());
	}
    return names;
  }
}
